package operatebag;

import people.Student;
import mysqloperate.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Random;

public class addstudent extends JFrame {

    private JLabel name;
    private JLabel id;
    private JLabel sex;
    private JLabel birth;
    private JLabel math;
    private JLabel java;
    private JLabel pe;
    private JTextField nametxt;
    private JTextField idtxt;
    private JTextField sextxt;
    private JTextField birthday;
    private JTextField mathscore;
    private JTextField javascore;
    private JTextField pescore;

    private JButton addbutton;
    private JButton cancelbutton;
    String idstrxx;

    JFrame jf;

    public addstudent() throws Exception {
        setTitle("学生添加");
        setBounds(500,200,400,500);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jf=this;
        setLayout(new FlowLayout());
        Box b=Box.createVerticalBox();

        Box b1=Box.createHorizontalBox();
        name=new JLabel("姓名");
        nametxt=new JTextField(15);
        b1.add(name);
        b1.add(nametxt);

        Box b2=Box.createHorizontalBox();
        id=new JLabel("学号");
        idtxt=new JTextField(15);
        Random ra=new Random();
        LocalDate data=LocalDate.now();
        int y,m,d,x;
        y=data.getYear();
        m=data.getMonthValue();
        d=data.getDayOfMonth();
        x=ra.nextInt(9000);
        String radom=String.format("%04d",x);

        idstrxx=String.valueOf(y)+String.format("%02d",m)+String.format("%02d",d)+radom;

        if(searchstu.youwuid(idstrxx)==false){
            x=ra.nextInt(9000);
            radom=String.format("%04d",x);
            idstrxx=String.valueOf(y)+String.format("%02d",m)+String.format("%02d",d)+radom;
        }
        idtxt.setText(idstrxx);
        idtxt.setEditable(false);
        b2.add(id);
        b2.add(idtxt);

        Box b3=Box.createHorizontalBox();
        sex=new JLabel("性别");
        sextxt=new JTextField(15);
        b3.add(sex);
        b3.add(sextxt);

        Box b4=Box.createHorizontalBox();
        birth=new JLabel("出生日期");
        birthday=new JTextField(15);
        b4.add(birth);
        b4.add(birthday);

        Box b5=Box.createHorizontalBox();
        math=new JLabel("数学成绩");
        mathscore=new JTextField(5);
        b5.add(math);
        b5.add(mathscore);

        Box b6=Box.createHorizontalBox();
        java=new JLabel("java成绩");
        javascore=new JTextField(5);
        b6.add(java);
        b6.add(javascore);

        Box b7=Box.createHorizontalBox();
        pe=new JLabel("体育成绩");
        pescore=new JTextField(5);
        b7.add(pe);
        b7.add(pescore);

        Box b8=Box.createHorizontalBox();
        addbutton=new JButton("添加");
        cancelbutton=new JButton("取消");
        addbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String name=nametxt.getText().trim();
                String id=idstrxx;
                int sex;
                if(sextxt.getText().trim().equals("男"))
                {
                    sex=1;
                }else{
                    sex=0;
                }
                String birth=birthday.getText();
                int math=Integer.parseInt(mathscore.getText().trim());
                int java=Integer.parseInt(javascore.getText().trim());
                int pe=Integer.parseInt(pescore.getText().trim());
                Student student=new Student();
                student.setName(name);
                student.setStudentid(id);
                student.setSEX(sex);
                student.setBirth(birth);
                student.setMath(math);
                student.setJava(java);
                student.setPe(pe);

                addstudentmysql add=new addstudentmysql();
                try {
                    add.addstudent(student);
                    JOptionPane.showMessageDialog(jf, "添加成功","",JOptionPane.INFORMATION_MESSAGE);
                    jf.dispose();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(jf, "添加失败","",JOptionPane.WARNING_MESSAGE);
                    throw new RuntimeException(ex);
                }


            }
        });
        cancelbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });
        b8.add(addbutton);
        b8.add(cancelbutton);

        b.add(b1);
        b.add(Box.createVerticalStrut(25));
        b.add(b2);
        b.add(Box.createVerticalStrut(25));
        b.add(b3);
        b.add(Box.createVerticalStrut(25));
        b.add(b4);
        b.add(Box.createVerticalStrut(25));
        b.add(b5);
        b.add(Box.createVerticalStrut(25));
        b.add(b6);
        b.add(Box.createVerticalStrut(25));
        b.add(b7);
        b.add(Box.createVerticalStrut(25));
        b.add(b8);

        add(b);

        setVisible(true);
    }
}
